package com.leyou.cart.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.cart.entity.Cart;
import com.leyou.cart.interceptors.UserInfoInterceptor;
import com.leyou.common.auth.entity.UserInfo;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartService {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserInfoInterceptor userInfoInterceptor;


    public void addCart(Cart cart) {

        UserInfo userInfo = userInfoInterceptor.getUserInfo();

        String key = String.valueOf(userInfo.getId());

        //基于hash结构的key生成一个hash的操作的对象，其可以直接操作hash的第二层map
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(key);

        //获取hKey
        String hKey = String.valueOf(cart.getSkuId());

        if (ops.hasKey(hKey)){//key存在表示曾经加入过，修改数量
            //根据hKey获取hValue
            String hValue = ops.get(hKey);

            //把cart存储的json转换为cart对象
            Cart storeCart = JsonUtils.nativeRead(hValue, new TypeReference<Cart>() {
            });

            //修改数量
            cart.setNum(cart.getNum()+storeCart.getNum());
            //修改完成后保存
            ops.put(hKey,JsonUtils.toString(cart));

        }else{//尚未添加,直接添加,注意：cart不能直接添加需要转成json
            ops.put(hKey, JsonUtils.toString(cart));
        }

    }

    //查询返回当前用户所有的购物车信息，购物车中的数据不新鲜，要做的事情是，先拿到不新鲜的数据，取其skuId再去查询item服务
    public List<Cart> listCart() {

        UserInfo userInfo = userInfoInterceptor.getUserInfo();

        String key = String.valueOf(userInfo.getId());

        //获取当前用户的购物车信息
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(key);

        //获取到当前key对应的所有value,String====>Cart,stream,map,把cart信息json形式获取并转换成cart类型
        List<Cart> cartList = ops.values()
                .stream()
                .map(value -> {
            return JsonUtils.nativeRead(value, new TypeReference<Cart>() {
            });
        }).collect(Collectors.toList());
        return cartList;
    }

    public void modifyCart(Long skuId, Integer num) {

        UserInfo userInfo = userInfoInterceptor.getUserInfo();

        String key = String.valueOf(userInfo.getId());

        //获取当前用户的购物车操作对象
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(key);

        String hKey = String.valueOf(skuId);

        //获取并把json转化为storeCart
        Cart storeCart = JsonUtils.nativeRead(ops.get(hKey), new TypeReference<Cart>() {
        });

        //重新赋值数量信息
        storeCart.setNum(num);

        ops.put(hKey,JsonUtils.toString(storeCart));
    }

    public void deleteCartById(Long skuId) {

        UserInfo userInfo = userInfoInterceptor.getUserInfo();

        String key = String.valueOf(userInfo.getId());

        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(key);

        String hKey = String.valueOf(skuId);
        //删除指定key对应的id信息
        ops.delete(hKey);
    }

    //购物车合并
    public void mergeCart(List<Cart> cartList) {
        cartList.forEach(cart -> addCart(cart));
    }
}
